<?php

namespace Biz\service\Store;

use Biz\cache\store\StoreDataCenter as cStoreDataCenter;
use Biz\model\bigData\StoreDesignerUserContentDailyReport;
use Biz\model\bigData\StoreBackstageInfoDailyReport;
use Biz\model\bigData\StoreCardInfoDailyReport;
use Biz\model\bigData\StoreContentInfoDailyReport;
use Biz\model\bigData\StoreWikiInfoDailyReport;
use hhz\constant\redis\redisKeys;

class DataCenter
{

    const DEFAULT_PERIOD_DAYS = 7;

    const SYMBOL = [0 => '-', 1 => '+'];

    public static function getInfo($storeId, $targetDate, $periodDays = self::DEFAULT_PERIOD_DAYS)
    {
        $returnData = [
            'sum' => [
                'expose' => [
                    'value' => '0',
                    'symbol' => '',
                    'ratio_text' => '',
                    'ratio' => '0',
                ],
                'pv' => [
                    'value' => '0',
                    'symbol' => '',
                    'ratio_text' => '',
                    'ratio' => '0',
                ],
                'consult' => [
                    'value' => '0',
                    'symbol' => '',
                    'ratio_text' => '',
                    'ratio' => '0',
                ],
                'up_fans' => [
                    'value' => '0',
                    'symbol' => '',
                    'ratio_text' => '',
                    'ratio' => '0',
                ],

            ],
            'detail' => [
                'store_content' => [
                    'sco_num' => "0",
                    'sco_pv_num' => "0",
                    'sco_expose_num' => "0",
                    'sco_comment_num' => "0",
                    'sco_share_num' => "0",
                    'sco_like_num' => "0",
                ],
                'designer_content' => [
                    'dco_num' => "0",
                    'dco_expose_num' => "0",
                    'dco_pv_num' => "0",
                ],
                'member_content' => [
                    'mco_num' => "0",
                    'mco_expose_num' => "0",
                    'mco_pv_num' => "0",
                ],
                'store_wiki' => [
                    'sw_num' => "0",
                    'sw_expose_num' => "0",
                    'sw_collect_num' => "0",
                    'sw_share_num' => "0",
                    'sw_pv_num' => "0",
                ],
                'store_card' => [
                    'sca_consult_num' => "0",
                    'sca_expose_num' => "0",
                    'sca_visit_num' => "0",
                ],
            ],
        ];

        $cacheData = cStoreDataCenter::getStatData($storeId, $targetDate, $periodDays);

        if (!empty($cacheData)) {
            return $cacheData;
        }

        $currentData = StoreBackstageInfoDailyReport::getPeriodData($storeId, $targetDate, $periodDays);

        $lastPeriodDay = 0 - $periodDays;

        $lastPeriodDate = date('Y-m-d', strtotime("$lastPeriodDay days", strtotime($targetDate)));

        $lastPeriodData = StoreBackstageInfoDailyReport::getPeriodData($storeId, $lastPeriodDate, $periodDays);

        if ($currentData) {

            [$exposeSymbol, $exposePercent, $exposePercentSymbol] = self::getPercent($currentData['store_exp_num'],
                $lastPeriodData['store_exp_num']);

            $returnData['sum']['expose'] = [
                'value' => $currentData['store_exp_num'],
                'symbol' => $exposeSymbol,
                'ratio_text' => $exposePercentSymbol,
                'ratio' => $exposePercent,
            ];

            [$pvSymbol, $pvPercent, $pvPercentSymbol] = self::getPercent($currentData['store_pv'],
                $lastPeriodData['store_pv']);

            $returnData['sum']['pv'] = [
                'value' => $currentData['store_pv'],
                'symbol' => $pvSymbol,
                'ratio_text' => $pvPercentSymbol,
                'ratio' => $pvPercent,
            ];

            [$consultSymbol, $consultPercent, $consultPercentSymbol] = self::getPercent($currentData['consult_num'],
                $lastPeriodData['consult_num']);

            $returnData['sum']['consult'] = [
                'value' => $currentData['consult_num'],
                'symbol' => $consultSymbol,
                'ratio_text' => $consultPercentSymbol,
                'ratio' => $consultPercent,
            ];

            [$fansSymbol, $fansPercent, $fansPercentSymbol] = self::getPercent($currentData['fans_num'],
                $lastPeriodData['fans_num']);

            $returnData['sum']['up_fans'] = [
                'value' => $currentData['fans_num'],
                'symbol' => $fansSymbol,
                'ratio_text' => $fansPercentSymbol,
                'ratio' => $fansPercent,
            ];
        }

        $storeContent = StoreContentInfoDailyReport::getPeriodData($storeId, $targetDate, $periodDays);

        if ($storeContent) {
            $returnData['detail']['store_content'] = [
                'sco_num' => $storeContent['content_num'],
                'sco_pv_num' => $storeContent['pv'],
                'sco_expose_num' => $storeContent['exp_num'],
                'sco_comment_num' => $storeContent['comment_num'],
                'sco_share_num' => $storeContent['share_num'],
                'sco_like_num' => $storeContent['like_num'],
            ];
        }

        $designerContent = StoreDesignerUserContentDailyReport::getPeriodData($storeId, $targetDate, $periodDays);

        if ($designerContent) {
            $returnData['detail']['designer_content'] = [
                'dco_num' => $designerContent['designer_content_num'],
                'dco_expose_num' => $designerContent['designer_exp_num'],
                'dco_pv_num' => $designerContent['designer_content_pv'],
            ];

            $returnData['detail']['member_content'] = [
                'mco_num' => $designerContent['user_content_num'],
                'mco_expose_num' => $designerContent['user_exp_num'],
                'mco_pv_num' => $designerContent['user_content_pv'],
            ];
        }

        $storeWiki = StoreWikiInfoDailyReport::getPeriodData($storeId, $targetDate, $periodDays);

        if ($storeWiki) {
            $returnData['detail']['store_wiki'] = [
                'sw_num' => $storeWiki['wiki_num'],
                'sw_expose_num' => $storeWiki['exp_num'],
                'sw_collect_num' => $storeWiki['favorite_num'],
                'sw_share_num' => $storeWiki['share_num'],
                'sw_pv_num' => $storeWiki['pv'],
            ];
        }

        $storeCard = StoreCardInfoDailyReport::getPeriodData($storeId, $targetDate, $periodDays);

        if ($storeCard) {
            $returnData['detail']['store_card'] = [
                'sca_consult_num' => $storeCard['im_uv'],
                'sca_expose_num' => $storeCard['card_exp_num'],
                'sca_visit_num' => $storeCard['homepage_pv'],
            ];
        }

        cStoreDataCenter::setStatData($storeId, $returnData, $targetDate, $periodDays);

        return $returnData;

//        $key =  sprintf("%s%s_%d", $constKey, $targetDate, (int)$periodDays);
//        $redis->setCache($storeId, json_encode($returnData, JSON_THROW_ON_ERROR),$key);
//        $redis->redisExpire(24 * 3600,$key);

    }


    private static function getPercent($curentCount, $oldCount)
    {
        $flag = $curentCount >= $oldCount ? 1 : 0;
        $symbol = self::SYMBOL[$flag];
        if ($oldCount == 0) {
            $percent = 0.00;
        } else {
            $percent = round(($curentCount - $oldCount) / $oldCount, 4);
        }
        $percentSymbol = sprintf("%01.2f%%", $percent * 100);
        $percent = strval(number_format($percent, 2));
        return [$symbol, $percent, $percentSymbol];
    }

}
